1 // Fig. 3.10: fig03_10.cpp 2 // Craps 3 #include 4 #include 5 #include 6 7 int rollDice( void ); // function prototype 8 9 int main() 10 { 11 enum Status { CONTINUE, WON, LOST }; 12 int sum, myPoint; 13 Status gameStatus; 14 15 srand( time( NULL ) ); 16 sum = rollDice(); // first roll of the dice 17 18 switch ( sum ) { 19 case 7: 20 case 11: // win on first roll 21 gameStatus = WON; 22 break; 23 case 2: 24 case 3: 25 case 12: // lose on first roll 26 gameStatus = LOST; 27 break; 28 default: // remember point 29 gameStatus = CONTINUE; 30 myPoint = sum; 31 cout << "Point is " << myPoint << endl; 32 break; // optional 33 } 34 35 while ( gameStatus == CONTINUE ) { // keep rolling 36 sum = rollDice(); 37 38 if ( sum == myPoint ) // win by making point 39 gameStatus = WON; 40 else 41 if ( sum == 7 ) // lose by rolling 7 42 gameStatus = LOST; 43 } 44 45 if ( gameStatus == WON ) 46 cout << "Player wins" << endl; 47 else 48 cout << "Player loses" << endl; 49 50 return 0; 51 } 52 53 int rollDice( void ) 54 { 55 int die1, die2, workSum; 56 57 die1 = 1 + rand() % 6; 58 die2 = 1 + rand() % 6; 59 workSum = die1 + die2; 60 cout << "Player rolled " << die1 << " + " << die2 61 << " = " << workSum << endl; 62 63 return workSum; 64 }